package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.FoodDAO;
import com.neutech.entity.Food;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDAOImpl implements FoodDAO {
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Food> foodList = new ArrayList<>();

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from food where businessId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,businessId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Food food = new Food();
                food.setId(resultSet.getInt("id"));
                food.setFoodName(resultSet.getString("foodName"));
                food.setFoodExplain(resultSet.getString("foodExplain"));
                food.setFoodImg(resultSet.getString("foodImg"));
                food.setFoodPrice(resultSet.getBigDecimal("foodPrice"));
                food.setBusinessId(resultSet.getInt("businessId"));


                foodList.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return foodList;
    }

    @Override
    public Food getById(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Food food = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from food where Id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                food = new Food();
                food.setId(resultSet.getInt("Id"));
                food.setFoodName(resultSet.getString("foodName"));
                food.setFoodExplain(resultSet.getString("foodExplain"));
                food.setFoodImg(resultSet.getString("foodImg"));
                food.setFoodPrice(resultSet.getBigDecimal("foodPrice"));
                food.setBusinessId(resultSet.getInt("businessId"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return food;
    }
}
